package middleWare

import (
	"github.com/gin-gonic/gin"
	//"net/http"
)

// func CORSMiddleware() gin.HandlerFunc {
// 	return func(context *gin.Context) {
// 		context.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8082")
// 		context.Writer.Header().Set("Access-Control-Max-Age", "86400")
// 		context.Writer.Header().Set("Access-Control-Allow-Methods", "*")
// 		context.Writer.Header().Set("Access-Control-Allow-Headers", "*")
// 		context.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

// 		if context.Request.Method == http.MethodOptions {
// 			context.AbortWithStatus(200)
// 		} else {
// 			context.Next()
// 		}
// 	}
// }

func CORSMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Header("Access-Control-Allow-Methods", "POST, GET, DELETE,PUT,OPTIONS")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}

		c.Next()
	}
}
